//
//  PaddingLabel.swift
//  Common
//
//  Created by 我骑着蜗牛走天下 on 2025/9/20.
//

import UIKit

open class PaddingLabel: UILabel {
    
    var textInsets: UIEdgeInsets
    
    public init(textInsets: UIEdgeInsets = .zero) {
        self.textInsets = textInsets
        super.init(frame: .zero)
    }
    
    required public init?(coder: NSCoder) {
        self.textInsets = .zero
        super.init(coder: coder)
    }
    
    public override func drawText(in rect: CGRect) {
        let insetRect = rect.inset(by: textInsets)
        super.drawText(in: insetRect)
    }

    public override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + textInsets.left + textInsets.right,
                      height: size.height + textInsets.top + textInsets.bottom)
    }
    
    // 如果使用 Auto Layout，重写 sizeThatFits
    public override func sizeThatFits(_ size: CGSize) -> CGSize {
        let insetSize = CGSize(width: size.width - textInsets.left - textInsets.right,
                               height: size.height - textInsets.top - textInsets.bottom)
        let fittingSize = super.sizeThatFits(insetSize)
        return CGSize(width: fittingSize.width + textInsets.left + textInsets.right,
                      height: fittingSize.height + textInsets.top + textInsets.bottom)
    }
}
